#include <pthread.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int cnt = 100;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;
void *threadrun(void *args)
{
    string name = static_cast<const char *>(args);
    while (1)
    {
        pthread_mutex_lock(&lock);
        pthread_cond_wait(&gcond, &lock);
        cout << name << "计算：" << cnt << endl;
        cnt++;
        pthread_mutex_unlock(&lock);
    }
}
int main()
{
    vector<pthread_t> threads;
    for (int i = 0; i < 5; i++)
    {
        pthread_t tid;
        char *name = new char[64];
        snprintf(name, 64, "thread-%d", i);
        int n = pthread_create(&tid, nullptr, threadrun, name);
        if (n != 0)
            continue;
        threads.push_back(tid);
        sleep(1);
    }
    sleep(3);
    while (1)
    {
        // cout<<"唤醒所有线程。。。"<<endl;
        // pthread_cond_broadcast(&gcond);
        cout << "唤醒一个线程。。。" << endl;
        pthread_cond_signal(&gcond);
        sleep(1);
    }
    for (auto &id : threads)
    {
        int m = pthread_join(id, nullptr);
        (void)m;
    }
    return 0;
}